<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img
        :src="imgBaseUrl + '@/assets/img/banner.4c6b6225.png'"
        width="100%"
        alt=""
      />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: spanNumIndex == index }"
            v-for="(item, index) in spanNum"
            :key="index"
            @click="changeActive(item.min, item.max, index)"
            >{{ item.txt2 }}</span
          >
          <!-- <span class="active">全部</span> -->
          <!-- <span>我还可以兑换</span>
          <span>0-500个</span>
          <span>500-1000个</span>
          <span>1000-1500个</span>
          <span>1500-2500个</span> -->
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: params.type == index }"
            v-for="(item, index) in typeNum"
            :key="index"
            @click="changeType(item.num, index)"
          >
            {{ item.txt }}
          </span>
        </li>
      </ul>
      <!-- {{ inpTxt }} -->
      <List :arr="arrNew" />
      <!-- {{ isReach ? "加载完毕" : "正在加载中..." }} -->
      <div class="center">
        <!-- <i class="icon-loading iconfont myloading" v-show="!isReach"></i> -->
        <!-- 全局loading  true-展示  false-隐藏 -->
        <!-- <div v-loading="!isReach"></div> -->
        <!-- 局部load  	 true-展示  false-隐藏 -->
        <!-- <div v-load="!isReach"></div> -->
        <el-table v-loading="!isReach" :data="[1]" style="width: 100%">
          <el-table-column label="已经到底啦~" width="1200" />
        </el-table>
        <!-- <span v-show="isReach">已经到底啦~ </span> -->
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { goodProductApi } from "./Api";
import {
  reactive,
  toRefs,
  ref,
  getCurrentInstance,
  watch,
  onMounted,
} from "vue";
import List from "@/components/List.vue";
import { useRoute } from "vue-router";
import { getClientHeight, getScrollHeight, getScrollTop } from "@/utils";
const { proxy } = getCurrentInstance() as any;
const imgBaseUrl = proxy.$imgUrl;
// 初始积分数选中
const spanNumIndex = ref(0);
// 初始类型选中
// const typeIndex = ref(0);
// 页码：
const page = ref(1);
// 每页8条
const size = ref(8);
// 是否加载下一页数据
const isLoading = ref(false);
// 是否加载完毕
let isReach = ref(false);
//要渲染的数组
let arr = ref<IRootObject[]>([]);
//是否显示正在加载
const typeNum = [
  {
    txt: "全部",
    num: 0,
  },
  {
    txt: "实体商品",
    num: 1,
  },
  {
    txt: "虚拟商品",
    num: 2,
  },
];
const spanNum = [
  {
    txt2: "全部",
  },
  {
    txt2: "我还可以兑换",
  },
  {
    min: "0",
    max: "500",
    txt2: "0-500个",
  },
  {
    min: "500",
    max: "1000",
    txt2: "500-1000个",
  },
  {
    min: "1000",
    max: "1500",
    txt2: "1000-1500个",
  },
  {
    min: "1500",
    max: "2500",
    txt2: "1500-2500个",
  },
];
const route = useRoute();
const arrNew = ref<IRootObject[]>([]);
const params = reactive<ICarts>({
  did: 0,
  type: 0,
  min: 0,
  max: 0,
  keyword: (route.query.keyword as string) || "",
});
//封装请求
const getData = () => {
  goodProductApi(params).then((res) => {
    if (res.code == 0) {
      arr.value = res.data;
      arrNew.value = arr.value.filter((item, index) => index < 8);
    }
  });
};
getData();
//修改数值
const changeActive = (min?: string, max?: string, index?: number) => {
  if (min) params.min = parseInt(min);
  if (max) params.max = parseInt(max);
  if (index || index == 0) spanNumIndex.value = index;
  // console.log(params, "params");
  getData();
};
// const reqProductAll = (params: any, a: any, b: any) => {};
//修改类型
const changeType = (number: number, index: number) => {
  if (number || number == 0) params.type = number;
  // console.log(params.type, number, index);
  getData();
};
watch(
  () => route.query,
  () => {
    if (route.query.keyword) {
      params.keyword = route.query.keyword as string;
    } else {
      params.keyword = "";
    }
    getData();
  }
);
onMounted(() => {
  window.addEventListener("scroll", addList);
});
const addList = () => {
  console.log(
    "滚动了",
    // 滚动距离
    getScrollTop(),
    // 可见高度：最大为浏览器高度
    getClientHeight(),
    // 文档高度=最大浏览器高度+最大滚动距离
    // 1.到达底部时：
    // 文档高度=浏览器高度(最大)+滚动距离(最大)
    // 2.到达距离底部页脚时
    // 文档高度-页脚=浏览器高度+滚动距离
    getScrollHeight()
  );

  if (getClientHeight() + getScrollTop() >= getScrollHeight() - 350) {
    let size = 8;
    console.log("可以渲染页面了");
    // 判断是否加载完毕
    // console.log(arr.value.length, arrNew.value.length);
    if (arr.value.length == arrNew.value.length) {
      isReach.value = true;
      return;
    }
    // 1.是否加载下一页数据
    if (!isLoading.value) {
      isLoading.value = true;
      page.value++;
      //2.没有加载，可以执行渲染
      // 3.加定时，异步请求
      setTimeout(() => {
        for (let i = (page.value - 1) * size; i < page.value * size; i++) {
          console.log(page, i, arr.value[i]);
          if (arr.value[i]) {
            arrNew.value.push(arr.value[i]);
          }
          // arr[i] && arrNew.push(arr[i]);
        }
        isLoading.value = false;
      }, 2000);
    }
  }
};

// const addList = (params: any, a: any, b: any) => {};
</script>

<style lang="less" scoped>
@import "@/assets/css/public.less";
:deep(.cell) {
  text-align: center;
}
:deep(.el-table th.el-table__cell.is-leaf) {
  border-bottom: none;
}
:deep(.el-table__inner-wrapper) {
  height: 39px;
}
.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          font-weight: bold;
          color: rgb(10, 50, 142);
        }
      }
    }
  }
}

.myloading {
  display: inline-block;
  animation: myloading 1s linear infinite;
}
@keyframes myloading {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
.center {
  display: flex;
  justify-content: center;
}
</style>
